/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.plugins  
 */
package com.apache.bid.service.plugins;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.Constant;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.common.vo.BidItemCacheVo;
import com.apache.bid.entity.BidItem;
import com.apache.bid.manager.BidItemManager;
import com.apache.bid.manager.BidSessionManager;
import com.apache.cache.util.Validator;

/**
 * description:  发布标的
 * @author Hou Dayu 创建时间：2016-3-24  
 * 
 */
public class UnityStartItemPluginImpl implements PluginConnector {

	protected Logger log = Logger.getLogger(getClass());
	protected BidSessionManager bidSessionManager;
	protected BidItemManager bidItemManager;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.manager.PluginConnector#execute(com.zbxsoft.api.vo.ParamsVo)  
	 */
	@SuppressWarnings("rawtypes")
	public Object execute(ParamsVo param) throws Exception {
		String itemId = Validator.getDefaultStr(param.getInfoId(), String.valueOf(param.getParams("infoId")));
		log.info("开始发布标的，加载缓存...");
		ResultEntity entity = new ResultEntity();
		if (Validator.isNull(itemId)) {
			entity.setResult("false");
			entity.setEntity("false");
			entity.setMessage("缺少方法参数，标的发布失败");
			return entity;
		}
		ParamsVo<BidItem> vo = new ParamsVo<BidItem>();
		vo.setInfoId(itemId);
		vo.setMethodKey("ByIdVo");
		BidItemCacheVo bidItemCacheVo = (BidItemCacheVo) bidItemManager.getInfoById(vo);
		if (Validator.isEmpty(bidItemCacheVo)) {
			entity.setResult("false");
			entity.setEntity("false");
			entity.setMessage("没有获取到标的信息，发布操作失败");
			return entity;
		}
		if (!"0".equals(bidItemCacheVo.getSessionId())) {
			BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("session");
			if (!cache.checkCacheObject(BidCacheManager.session_prefix + bidItemCacheVo.getSessionId())) {
				entity.setResult("false");
				entity.setMessage("活动还没有发布，标的不能发布");
				return entity;
			}
		} else {
			String flag = UnityItemPluginHelper.getInstance().checkPublish(itemId, "");
			if (Validator.isNotNull(flag)) {//判断是否有发布的条件
				entity.setResult("false");
				entity.setEntity("false");
				entity.setMessage(flag);
				return entity;
			}
		}
		String status = bidItemCacheVo.getItemStatus();
		if (Constant.getInstance().getSessionConfig("statusPubslish").equals(status)) {
			entity.setResult("false");
			entity.setEntity("false");
			entity.setMessage("此标的已经发布，不需要重复发布操作");
			return entity;
		}

		//更新缓存+数据库状态
		UnityItemPluginHelper.getInstance().startItem(bidItemCacheVo, true);
		entity.setMessage("操作成功");
		entity.setEntity("true");
		entity.setResult("true");
		return entity;
	}

	public void setBidSessionManager(BidSessionManager bidSessionManager) {
		this.bidSessionManager = bidSessionManager;
	}

	public void setBidItemManager(BidItemManager bidItemManager) {
		this.bidItemManager = bidItemManager;
	}

}
